<?php
class Gold_Productprint_ProductController extends Mage_Adminhtml_Controller_Action{

    public function indexAction(){

        $activeCategories = array();
        foreach (Mage::helper('catalog/category')->getStoreCategories() as $child) {
            if ($child->getIsActive()) {
                $activeCategories[] = $child;
            }
        }
        $activeCategoriesCount = count($activeCategories);
        $categoryInstance = Mage::getModel('catalog/category');
        $categoryHtml = '';
        foreach( $activeCategories as $category){

            // get all children
            if (Mage::helper('catalog/category_flat')->isEnabled()) {
                $children = (array)$category->getChildrenNodes();
                $childrenCount = count($children);
            } else {
                $children = $category->getChildren();
                $childrenCount = $children->count();
            }
            $hasChildren = ($children && $childrenCount);

            // select active children
            $activeChildren = array();
            foreach ($children as $child) {
                if ($child->getIsActive()) {
                    $activeChildren[] = $child;
                }
            }
            //$url = $categoryInstance->setData($category->getData())->getUrl();
            $url = Mage::getUrl('*/*/print',array('id'=>$category->getId()));
            $activeChildrenCount = count($activeChildren);
            if($activeChildrenCount >0){
                $categoryHtml .= '<ul class="mar_t2"><li><span><a target="_blank" href="'.$url.'" title="'.$category->getName().'" >'.$category->getName().'</a></span>';
                $categoryHtml .='<ul class="mar_t1" style="padding-left:5px;">';
                $i =0;
                foreach($activeChildren as $subcategory){
                    $i++;
                    if($i==11){
                        // $categoryHtml .='<li><a href="'.$url.'">view all</a></li>';                     
                    }
                    //$suburl = $categoryInstance->setData($subcategory->getData())->getUrl();
                    $suburl = Mage::getUrl('*/*/print',array('id'=>$subcategory->getId()));
                    $categoryHtml .='<li><a target="_blank" href="'.$suburl.'" title="'.$subcategory->getName().'" >'.$subcategory->getName().'</a>';

                    // get all children
                    if (Mage::helper('catalog/category_flat')->isEnabled()) {
                        $children = (array)$subcategory->getChildrenNodes();
                        $childrenCount = count($children);
                    } else {
                        $children = $subcategory->getChildren();
                        $childrenCount = $children->count();
                    }
                    $hasChildren = ($children && $childrenCount);

                    // select active children
                    $subactiveChildren = array();
                    foreach ($children as $child) {
                        if ($child->getIsActive()) {
                            $subactiveChildren[] = $child;
                        }
                    }
                    $activeChildrenCount = count($activeChildren);
                    if($activeChildrenCount>0){
                        $categoryHtml.='<ul class="mar_t3" style="padding-left:10px;">';
                        foreach($subactiveChildren as $subcategorySub){
                            $categoryHtml .='<li><a target="_blank" href="'. Mage::getUrl('*/*/print',array('id'=>$subcategorySub->getId())).'">'.$subcategorySub->getName().'</a></li>'; 
                        }
                        $categoryHtml .=' </ul>';
                    }
                    $categoryHtml .=' </li>';



                }
                $categoryHtml .= '</ul>';
                $categoryHtml .= '</li></ul>';
            }


        }
        $this->getResponse()->setBody($categoryHtml);

    }
    public function printAction(){
        $category_id = $this->getRequest()->getParam('id');
        if(!$category_id) $html =  'url参数出错';

        if($category_id){
            $category = Mage::getModel('catalog/category')->load($category_id); 
            if(!$category->getId()){

                $html =  '找不到分类';
            }else{
                $baseDir = Mage::getUrl().'media/'; 
                $productCollection = $category->getProductCollection();
                $html ='<h1>'.$category->getName().'</h1>';
                $html .= '<table style="border-collapse:collapse;border:none;width:1000;margin:0 auto;text-align:center">';
                $html .='<tr><th style="border:1px solid #ccc">Sku</th><th style="border:1px solid #ccc">Image</th><th style="border:1px solid #ccc">Name</th><th style="border:1px solid #ccc">Unit Price</th><th style="border:1px solid #ccc">Ref Cost</th><th style="border:1px solid #ccc">Shelf No</th><th style="border:1px solid #ccc">Weight</th><th style="border:1px solid #ccc">Qty</th><th style="border:1px solid #ccc">Status</th><th style="border:1px solid #ccc">Stock</th><th style="border:1px solid #ccc">Backorders</th></tr>';
                foreach($productCollection as $product){
                    $product = Mage::getModel('catalog/product')->load($product->getId());
                    $html .='<tr>';
                    $html .='<td style="border:1px solid #ccc">'.$product->getSku().'</td>';
                    $image =  $product->getSmallImage()?'<img width="100" src="'.Mage::helper('catalog/image')->init($product, 'small_image',$product->getSmallImage())->resize(100).'" />':'';
                    $html .='<td style="border:1px solid #ccc">'.$image.'</td>';
                    $html .='<td style="border:1px solid #ccc">'.$product->getName().'</td>';
                    $html .='<td style="border:1px solid #ccc">'.$product->getPrice().'</td>';
                    $html .='<td style="border:1px solid #ccc">'.$product->getRefCost().'</td>';
                    $html .='<td style="border:1px solid #ccc">'.$product->getShelfNo().'</td>';
                    $html .='<td style="border:1px solid #ccc">'.$product->getWeight().'</td>';
                    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
                    $html .='<td style="border:1px solid #ccc">'.($stockItem->getQty()*1).'</td>';
                      if ($product->getStatus()){ $status = 'Enabled';}else{ $status = 'Disabled';}
                    $html .='<td style="border:1px solid #ccc">'.$status.'</td>';
                    if ($product->isAvailable()){ $stock = 'In stock';}else{ $stock = 'Out of stock';}
                        $html .='<td style="border:1px solid #ccc">'.$stock.'</td>';
                    if($stockItem->getBackorders()==1){
                        $backorders='Allow';
                    }elseif($stockItem->getBackorders()==2){
                        $backorders='Allow and Notify Customer';
                    }else{
                         $backorders='No';
                    }
                     $html .='<td style="border:1px solid #ccc">'.$backorders.'</td>';
                    $html .='</tr>';
                }
                $html .= '</table>';
            }

        }
        $html = '<div style="text-align:center">'.$html.'</div>';
        $this->getResponse()->setBody($html);
    }
}

